package com.ehuoyun.ecstore.action;

import java.io.ByteArrayInputStream;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.StreamingResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import com.ehuoyun.ecstore.biz.AdvertisementMgr;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.Transform;

public class AdImageActionBean extends AbstractActionBean {
	private Long id;
	
    @SpringBean
    private AdvertisementMgr adMgr;

    public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@DefaultHandler
    public Resolution serveImage() {
    	ImagesService imagesService = ImagesServiceFactory.getImagesService();
        Image oldImage = ImagesServiceFactory.makeImage(adMgr.getAdImage(id).getBytes());
        Transform resize = ImagesServiceFactory.makeResize(200, 300);
        Image newImage = imagesService.applyTransform(resize, oldImage);
        	
        StreamingResolution streamingResolution = 
        	new StreamingResolution("image/jpeg", 
        			new ByteArrayInputStream(newImage.getImageData()));
        return streamingResolution.setFilename("");
    }

}
